import { _decorator, Component, Node, AudioClip, AudioSource } from 'cc';
const { ccclass, property } = _decorator;
interface IAudioMap {//map接口 用C++map理解就行
    [name: string]: AudioClip;
}
@ccclass('AudioManager')
export class AudioManager extends Component {
    @property([AudioClip])
    public audioList: AudioClip[] = [];//音效数组

    private dict: IAudioMap = {};//创建一个map
    private audioSourse: AudioSource = null;//音效组件

    start() {
        for (let i = 0; i < this.audioList.length; i++) {//为map赋值
            const element = this.audioList[i];
            this.dict[element.name] = element;
        }
        this.audioSourse = this.getComponent(AudioSource);//绑定音效组件
    }
    public play(name: string) {//播放
        const audioClip = this.dict[name];
        if (audioClip !== undefined) {
            this.audioSourse.playOneShot(audioClip);
        }
    }
    update(deltaTime: number) {

    }
}

